<?php

namespace App\Extend;

class Validator extends \Windward\Extend\Validator
{

    public function isNull($data)
    {
        return trim($data, " ") ? true : false;
    }

    public function checkAccountOnly($field, $vars = [])
    {
        $model = $this->container->model('Admin');
        $cond = ['account' => $field, 'deleted' => 0];
        $id = $vars['id'];
        if ($id) {
            $cond['[neq]id'] = $id;
        }
        if ($model->get('tb_admin', 'id', $cond)) {
            return false;
        }
        return true;
    }

    /**
     * 空串或空数组
     */
    public function isNotNull($field, $vars = [])
    {
        if (!isset($field) && $vars['isset_flag'] === true) {
            return true;
        }
        if (is_array($field)) {
            if ($vars['empty_flag'] === true && empty($field)) {
                return true;
            }
            if (empty($field)) {
                return false;
            }
        } else {
            if ($vars['empty_flag'] === true && strlen($field) === 0) {
                return true;
            }
            if (!strlen($field)) {
                return false;
            }
        }
        return true;
    }

    public function checkCharLength($field, $vars = [])
    {
        if ($vars['empty_flag'] === true && strlen($field) === 0) {
            return true;
        }
        if (isset($field)) {
            $field = preg_replace(['/\r\n/', '/\r/', '/\n/'], ' ', $field);
            $count = -1;
            if (isset($vars['max_length'])) {
                preg_match_all("/./us", $field, $match);
                $count = count($match[0]);
                if ($count > $vars['max_length']) {
                    return false;
                }
            }
            if (isset($vars['min_length'])) {
                if ($count == -1) {
                    preg_match_all("/./us", $field, $match);
                    $count = count($match[0]);
                }
                if ($count < $vars['min_length']) {
                    return false;
                }
            }
            if (isset($vars['length'])) {
                if ($count == -1) {
                    preg_match_all("/./us", $field, $match);
                    $count = count($match[0]);
                }
                if ($count != $vars['length']) {
                    return false;
                }
            }
            if (isset($vars['match_value'])) {
                if ($field != $vars['match_value']) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    public function isAccount($field, $vars = [])
    {
        if (!isset($field) && $vars['isset_flag'] === true) {
            return true;
        }
        if ($vars['empty_flag'] === true && strlen($field) === 0) {
            return true;
        }
        if (isset($field)) {
            return preg_match('/^[0-9a-zA-Z]{3,18}$/', $field);
        }
    }

    public function isPassword($field, $vars = [])
    {
        if (!isset($field) && $vars['isset_flag'] === true) {
            return true;
        }
        if ($vars['empty_flag'] === true && strlen($field) === 0) {
            return true;
        }
        if (isset($field)) {
            return preg_match('/^[0-9a-zA-Z]+$/', $field);
        }
    }

    public function isRobotExists($field, $vars = [])
    {
        $robotId = (int) $vars['id'];
        $robotModel = $this->container->model('Robot');
        $one = $robotModel->get('tb_robot', 'id', ['[neq]id' => $robotId, 'account' => $field]);
        return $one['id'] ? false : true;
    }

    public function checkCode($field, $vars = [])
    {
        return true;
        if (isset($_SESSION['sms_code'])) {
            if (time() <= $_SESSION['sms_code']['time'] && $_SESSION['sms_code']['code'] == md5($field . $vars['mobile'])) {
                return true;
            }
        }
        return false;
    }

}
